package cn.heygod.nacostool.web;

import cn.heygod.nacostool.bean.Message;
import cn.heygod.nacostool.config.DeployConfig;
import cn.heygod.nacostool.config.NacosConfig;
import cn.heygod.nacostool.session.SessionManager;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author Simon
 */
@Controller
@RequestMapping("/api")
public class LoginController {

    @Autowired
    private DeployConfig deployConfig;

    @Autowired
    private SessionManager sessionManager;

    @RequestMapping("/login")
    @ResponseBody
    public Message login(String username, String password, HttpServletRequest request){
        if(deployConfig.getUsername().equals(username)
           && deployConfig.getPassword().equals(password) ){
            sessionManager.login(request,username);
            return new Message(true);
        }
        return new Message(false,"帐号密码不匹配，无权限。");
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        sessionManager.logout(request);
        return "login.html";
    }

}
